//=====================================================================
//    COPYRIGHT(C) Innobeam
//    ALL RIGHTS RESERVED
//=====================================================================
//Filename    : Can_receive.v rev 1.0
//Created On  : 2024-4-2
//Author      : zhangxudong
//*Description:
	//*1.发送拼接规律： 标识+地址+48位数据
	//*2.OA是主控发到各个控制器的标志；OB是各个控制器到主控的标志
//Include     : 
//Modification: fei.yu
//=====================================================================
module Can_receive (		
					iClk			,	//
					iRst_n			,	//reset in low actove
            		iCanData_Receive,
					ovHHC_Gantry,
					ovHHC_Collimator,		//16bit output return	
					ovHHC_Swing,			//16bit output return		
					ovHHC_MLCX1,	//16bit output return			
					ovHHC_MLCX2,	//16bit output return			
					ovHHC_JawY1,	//16bit output return				
					ovHHC_JawY2,		//16bit output return		
					ovHHC_CouchVRT,		//16bit output return	
					ovHHC_CouchLAT,	//16bit output return
					ovHHC_CouchLNG,
					ovHHC_CouchLED1,
					ovHHC_CouchLED2,
					ovHHC_DisplayState,
					ovHHC_CouchLNGPLAN,
					ovHHC_MLCX,
					ovHHC_MLCY 						
			);
//========================================================================
//    parameter
//========================================================================

parameter		VERSION = 16'd10107;				//V1.01.06


//========================================================================
//    port
//========================================================================
input   						iClk;
input							iRst_n;
input           [63:0]          iCanData_Receive;

output		[15:0]		ovHHC_Gantry;
output		[15:0]		ovHHC_Collimator;		
output		[15:0]		ovHHC_Swing;		
output		[15:0]		ovHHC_MLCX1;		
output		[15:0]		ovHHC_MLCX2;		
output		[15:0]		ovHHC_JawY1;		
output		[15:0]		ovHHC_JawY2;	
output		[15:0]		ovHHC_CouchVRT;
output		[15:0]		ovHHC_CouchLAT;	
output		[15:0]		ovHHC_CouchLNG;
output		[15:0]		ovHHC_CouchLED1;
output		[15:0]		ovHHC_CouchLED2;
output		[15:0]		ovHHC_DisplayState;
output		[15:0]		ovHHC_CouchLNGPLAN;
output		[15:0]		ovHHC_MLCX;	
output		[15:0]		ovHHC_MLCY;	
//******************变量定义*********************//
reg		[15:0]		rvHHC_Gantry;
reg		[15:0]		rvHHC_Collimator;		
reg		[15:0]		rvHHC_Swing;		
reg		[15:0]		rvHHC_MLCX1;		
reg		[15:0]		rvHHC_MLCX2;		
reg		[15:0]		rvHHC_JawY1;		
reg		[15:0]		rvHHC_JawY2;	
reg		[15:0]		rvHHC_CouchVRT;
reg		[15:0]		rvHHC_CouchLAT;	
reg		[15:0]		rvHHC_CouchLNG;
reg		[15:0]		rvHHC_CouchLED1;
reg		[15:0]		rvHHC_CouchLED2;
reg		[15:0]		rvHHC_DisplayState;
reg		[15:0]		rvHHC_CouchLNGPLAN;
reg		[15:0]		rvHHC_MLCX;	
reg		[15:0]		rvHHC_MLCY;	
//标识和地址
wire        [7:0]           wvMark;
wire        [7:0]           wvAddr;   
//上升沿信号	
wire 		[7:0]	wvRise_T;
reg 		[7:0]	rvRise1_T;
reg 		[7:0]	rvRise2_T;
//*********************输出赋值*******************//					
assign     ovHHC_Gantry 		= rvHHC_Gantry;
assign     ovHHC_Collimator 	= rvHHC_Collimator;	
assign     ovHHC_Swing 			= rvHHC_Swing;		
assign     ovHHC_MLCX1 			= rvHHC_MLCX1;		
assign     ovHHC_MLCX2 			= rvHHC_MLCX2;		
assign     ovHHC_JawY1 			= rvHHC_JawY1;		
assign     ovHHC_JawY2 			= rvHHC_JawY2;
assign     ovHHC_CouchVRT 		= rvHHC_CouchVRT;	 		
assign     ovHHC_CouchLAT 		= rvHHC_CouchLAT;	
assign     ovHHC_CouchLNG 		= rvHHC_CouchLNG;			
assign     ovHHC_CouchLED1 		= rvHHC_CouchLED1;
assign     ovHHC_CouchLED2		= rvHHC_CouchLED2;
assign     ovHHC_DisplayState 	= rvHHC_DisplayState;
assign     ovHHC_CouchLNGPLAN 	= rvHHC_CouchLNGPLAN;
assign     ovHHC_MLCX 			= rvHHC_MLCX;	
assign     ovHHC_MLCY 			= rvHHC_MLCY;	

//*提取Can传输标志
assign    wvMark =     iCanData_Receive[63:56];
assign    wvAddr =    iCanData_Receive[55:48];

always @(posedge iClk or negedge iRst_n)begin
if(iRst_n == 1'b0)begin
	rvRise1_T<=8'd0;
	rvRise2_T<=8'd0;
	end
else begin
	rvRise1_T[1]<=(wvAddr==1);
	rvRise2_T[1]<=rvRise1_T[1];
	rvRise1_T[2]<=(wvAddr==2);
	rvRise2_T[2]<=rvRise1_T[2];
	rvRise1_T[3]<=(wvAddr==3);
	rvRise2_T[3]<=rvRise1_T[3];
	rvRise1_T[4]<=(wvAddr==4);
	rvRise2_T[4]<=rvRise1_T[4];
	rvRise1_T[5]<=(wvAddr==5);
	rvRise2_T[5]<=rvRise1_T[5];
	rvRise1_T[6]<=(wvAddr==6);
	rvRise2_T[6]<=rvRise1_T[6];
 end
end

assign wvRise_T[1]=rvRise1_T[1] & ~rvRise2_T[1];
assign wvRise_T[2]=rvRise1_T[2] & ~rvRise2_T[2];
assign wvRise_T[3]=rvRise1_T[3] & ~rvRise2_T[3];
assign wvRise_T[4]=rvRise1_T[4] & ~rvRise2_T[4];
assign wvRise_T[5]=rvRise1_T[5] & ~rvRise2_T[5];
assign wvRise_T[6]=rvRise1_T[6] & ~rvRise2_T[6];

//*提取Can总线内容-CAN 
always @(posedge iClk or negedge iRst_n)begin
if(iRst_n == 1'b0)begin
	  	rvHHC_Gantry            <= 16'd0;
		rvHHC_Collimator        <= 16'd0;		
		rvHHC_Swing             <= 16'd0;		
		rvHHC_MLCX1             <= 16'd0;		
		rvHHC_MLCX2             <= 16'd0;		
		rvHHC_JawY1             <= 16'd0;		
		rvHHC_JawY2             <= 16'd0;	
		rvHHC_CouchVRT          <= 16'd0;
		rvHHC_CouchLAT          <= 16'd0;	
		rvHHC_CouchLNG          <= 16'd0;
		rvHHC_CouchLED1         <= 16'd0;
		rvHHC_CouchLED2         <= 16'd0;
		rvHHC_DisplayState      <= 16'd0;
		rvHHC_CouchLNGPLAN      <= 16'd0;
		rvHHC_MLCX              <= 16'd0;	
		rvHHC_MLCY              <= 16'd0;
      end
else begin
	if(wvMark == 8'h0A)begin
		rvHHC_Gantry    <=(wvRise_T[1])?iCanData_Receive[15:0]:rvHHC_Gantry;
		rvHHC_Collimator<=(wvRise_T[1])?iCanData_Receive[31:16]:rvHHC_Collimator;
		rvHHC_Swing     <=(wvRise_T[1])?iCanData_Receive[47:32]:rvHHC_Swing;

		rvHHC_MLCX1<=(wvRise_T[2])?iCanData_Receive[15:0]:rvHHC_MLCX1;
		rvHHC_MLCX2<=(wvRise_T[2])?iCanData_Receive[31:16]:rvHHC_MLCX2;
		rvHHC_JawY1<=(wvRise_T[2])?iCanData_Receive[47:32]:rvHHC_JawY1;

		rvHHC_JawY2<=   (wvRise_T[3])?iCanData_Receive[15:0]:rvHHC_JawY2;
		rvHHC_CouchVRT<=(wvRise_T[3])?iCanData_Receive[31:16]:rvHHC_CouchVRT;
		rvHHC_CouchLAT<=(wvRise_T[3])?iCanData_Receive[47:32]:rvHHC_CouchLAT;

		rvHHC_CouchLNG<= (wvRise_T[4])?iCanData_Receive[15:0]:rvHHC_CouchLNG;
		rvHHC_CouchLED1<=(wvRise_T[4])?iCanData_Receive[31:16]:rvHHC_CouchLED1;
		rvHHC_CouchLED2<=(wvRise_T[4])?iCanData_Receive[47:32]:rvHHC_CouchLED2;	

		rvHHC_DisplayState<=(wvRise_T[5])?iCanData_Receive[15:0]:rvHHC_DisplayState;
		rvHHC_CouchLNGPLAN<=(wvRise_T[5])?iCanData_Receive[31:16]:rvHHC_CouchLNGPLAN;
		rvHHC_MLCX        <=(wvRise_T[5])?iCanData_Receive[47:32]:rvHHC_MLCX;	

		rvHHC_MLCY<=(wvRise_T[6])?iCanData_Receive[15:0]:rvHHC_MLCY;	
		end
	else begin
	  	rvHHC_Gantry            <= rvHHC_Gantry;
		rvHHC_Collimator        <= rvHHC_Collimator;		
		rvHHC_Swing             <= rvHHC_Swing;		
		rvHHC_MLCX1             <= rvHHC_MLCX1;		
		rvHHC_MLCX2             <= rvHHC_MLCX2;		
		rvHHC_JawY1             <= rvHHC_JawY1;		
		rvHHC_JawY2             <= rvHHC_JawY2;	
		rvHHC_CouchVRT          <= rvHHC_CouchVRT;
		rvHHC_CouchLAT          <= rvHHC_CouchLAT;	
		rvHHC_CouchLNG          <= rvHHC_CouchLNG;
		rvHHC_CouchLED1         <= rvHHC_CouchLED1;
		rvHHC_CouchLED2         <= rvHHC_CouchLED2;
		rvHHC_DisplayState      <= rvHHC_DisplayState;
		rvHHC_CouchLNGPLAN      <= rvHHC_CouchLNGPLAN;
		rvHHC_MLCX              <= rvHHC_MLCX;	
		rvHHC_MLCY              <= rvHHC_MLCY;
		end
	end
end
endmodule

